/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Control;
import Logic.Log;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author LiN
 */
public class GUI_Main extends javax.swing.JFrame {

    private JPanel panel_DBLink;
    private JPanel panel_ClasificationCataloge;
    private JPanel panel_Users;
    private JPanel panel_ServerRules;
    private JPanel panel_RulesControl;
    private Control control;

    /**
     * Creates new form GUI_Main
     */
    public GUI_Main(String user, String pass, String ip, String port, String db) {
        setTitle("Security and Audit Manager");
        //setExtendedState(MAXIMIZED_BOTH);
        
        
        ImageIcon img = new ImageIcon("icon6.png");
        this.setIconImage(img.getImage());
        

        control = Control.getInstance(user, pass, ip, port, db);
        initComponents();
        initTabbedPane();
        control.loadLogs();

        setVisible(true);
        centreWindow(this);
        
        //Logs
        Log log = new Log(new Date(),user,"LOCAL",Log.CONNECTION,"Connected: "+user+"/"+ip+"/"+port+"/"+db);
        control.insertLog(log);
        control.saveLogs();
    }
    
    public static void centreWindow(Window frame) {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }
    
    public void setEnableAll(){
        getjTabbedPane1().setEnabledAt(1,true);
        getjTabbedPane1().setEnabledAt(2,true);
        getjTabbedPane1().setEnabledAt(3,true);
        getjTabbedPane1().setEnabledAt(4,true);
    }
    public void setDisableAll(){
        getjTabbedPane1().setEnabledAt(1,false);
        getjTabbedPane1().setEnabledAt(2,false);
        getjTabbedPane1().setEnabledAt(3,false);
        getjTabbedPane1().setEnabledAt(4,false);
    }
    
    public void initTabbedPaneSecurity(){
        this.getjTabbedPane1().remove(panel_ServerRules);
        this.getjTabbedPane1().remove(panel_RulesControl);
        this.getjTabbedPane1().remove(panel_ClasificationCataloge);//agregue esto por un bug que habia
        this.getjTabbedPane1().remove(panel_Users);//agregue esto por un bug que habia
        
        this.panel_ClasificationCataloge = new GUI_ClasificationCataloge();
        this.panel_Users = new GUI_Users();
        this.getjTabbedPane1().addTab("2. Clasification Cataloge", panel_ClasificationCataloge);
        this.getjTabbedPane1().addTab("3. Users", panel_Users);
        
        this.getjTabbedPane1().setSelectedIndex(1);//Agregue esto
        
//        jTabbedPane1.setComponentAt(1,  panel_ClasificationCataloge);
//        jTabbedPane1.setComponentAt(2, panel_Users);
    }
    
    public void initTabbedPaneAudit(){
        this.getjTabbedPane1().remove(panel_ServerRules);//agregue esto por un bug que habia
        this.getjTabbedPane1().remove(panel_RulesControl);//agregue esto por un bug que habia
        this.getjTabbedPane1().remove(panel_ClasificationCataloge);
        this.getjTabbedPane1().remove(panel_Users);
        this.panel_ServerRules = new GUI_Rules();
        this.panel_RulesControl = new GUI_Logminer();
        this.getjTabbedPane1().addTab("2. Rules", panel_ServerRules);
        this.getjTabbedPane1().addTab("3. Audit", panel_RulesControl);
        
        this.getjTabbedPane1().setSelectedIndex(1);//Agregue esto
        
//        jTabbedPane1.setComponentAt(1,  panel_ServerRules);
//        jTabbedPane1.setComponentAt(2, panel_RulesControl);
    }

    public void initTabbedPane() {
        this.panel_ClasificationCataloge = new GUI_ClasificationCataloge();
        this.panel_DBLink = new GUI_DBLink(this);
        this.panel_Users = new GUI_Users();
        this.panel_RulesControl = new GUI_Logminer();
        this.panel_ServerRules = new GUI_Rules();
        
        this.getjTabbedPane1().addTab("1. Server Selection", panel_DBLink);
//        this.jTabbedPane1.addTab("2. Clasification Cataloge", panel_ClasificationCataloge);
//        this.jTabbedPane1.addTab("3. Users", panel_Users);
//        this.jTabbedPane1.addTab("4. Rules", panel_ServerRules);
//        this.jTabbedPane1.addTab("5. Rules Control", panel_RulesControl);
        

        //this.setDisableAll();
        
//        ChangeListener changeListener = new ChangeListener() {
//            @Override
//            public void stateChanged(ChangeEvent changeEvent) {
//                JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
//                int index = sourceTabbedPane.getSelectedIndex();
//
//                switch (index) {
//                    case 0:
//                        //panel_DBLink=new GUI_DBLink();
//                        jTabbedPane1.setComponentAt(index, panel_DBLink);
//                        break;
//                    case 1:
//                        panel_ClasificationCataloge = new GUI_ClasificationCataloge();
//                        jTabbedPane1.setComponentAt(index, panel_ClasificationCataloge);
//                        break;
//                    case 2:
//                        panel_Users = new GUI_Users();
//                        jTabbedPane1.setComponentAt(index, panel_Users);
//                        break;
//                    case 3:
//                        jTabbedPane1.setComponentAt(index, panel_ServerRules);
//                        break;
//                    case 4:
//                        //panel_RulesControl = new GUI_Logminer();
//                        jTabbedPane1.setComponentAt(index, panel_RulesControl);
//                        break;
//
//                }
//                //System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));                
//            }
//        };
        //this.jTabbedPane1.addChangeListener(changeListener);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem1 = new javax.swing.JMenuItem();
        securityMenu = new javax.swing.JMenu();
        auditMenu = new javax.swing.JMenu();
        logsMenu = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("File");
        jMenu1.add(jSeparator1);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        securityMenu.setText("Security Control");
        securityMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                securityMenuMouseClicked(evt);
            }
        });
        jMenuBar1.add(securityMenu);

        auditMenu.setText("Audit");
        auditMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                auditMenuMouseClicked(evt);
            }
        });
        jMenuBar1.add(auditMenu);

        logsMenu.setText("Logs");
        logsMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                logsMenuMouseClicked(evt);
            }
        });
        jMenuBar1.add(logsMenu);

        jMenu2.setText("Help");

        jMenuItem2.setText("About");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 950, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
       GUI_About g = new GUI_About();
       g.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void securityMenuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_securityMenuMouseClicked
        if(!control.getSELECTED_SERVER().equals(""))
        initTabbedPaneSecurity();
        else
            JOptionPane.showMessageDialog(this,"No Server Selected...!!!");
    }//GEN-LAST:event_securityMenuMouseClicked

    private void auditMenuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_auditMenuMouseClicked
        if(!control.getSELECTED_SERVER().equals(""))
        initTabbedPaneAudit();
        else
            JOptionPane.showMessageDialog(this,"No Server Selected...!!!");
    }//GEN-LAST:event_auditMenuMouseClicked

    private void logsMenuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logsMenuMouseClicked
        GUI_Log log = new GUI_Log();
        log.setVisible(true);
    }//GEN-LAST:event_logsMenuMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new GUI_Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu auditMenu;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JMenu logsMenu;
    private javax.swing.JMenu securityMenu;
    // End of variables declaration//GEN-END:variables

    public javax.swing.JTabbedPane getjTabbedPane1() {
        return jTabbedPane1;
    }
}
